package org.lilred.ui;

import org.lilred.ui.theme.ThemeOld;
import org.lilred.ui.theme.ThemeSetting;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;

public class AccordionManager extends Manager implements FieldChangeListener {
	int selectedField;
	Field titleField;
	ThemeOld themeOld;
	private boolean collapseable = true;
	boolean autoOpen = true;
	boolean multipleFieldsOpenSimultaneously = false;
	ThemeSetting clickableSetting;
	ThemeSetting hoverSetting;
	ThemeSetting activeSetting;
	ThemeSetting contentSetting;
	public int animationDuration = 250;
	
	public AccordionManager(ThemeOld themeOld, long style) {
		super(style);
		this.themeOld = themeOld;
	}
	
	public void add(CollapseableThemedField collapseableThemedField) {
		if (autoOpen && getFieldCount() == 0) collapseableThemedField.expand();
		collapseableThemedField.setChangeListener(this);
		super.add(collapseableThemedField);
	}
	
	public void add(Field f) {
		throw new IllegalArgumentException("AccordionManager can only hold CollapseableDecoratedFields");
	}
	
	public int getPreferredHeight() {
		int h = 0;
		for (int i = 0; i < getFieldCount(); i++) {
			Field f = getField(i);
			h += f.getPreferredHeight();
		}
		return h;
	}

	/*
	 * Field change listener for title fields.  If one of them is clicked, that field becomes active
	 */
	public void fieldChanged(Field field, int context) {
		int numExpandedFields = 0;
		//Count the number of currently expanded fields
		for (int i = 0; i < getFieldCount(); i++) 
			if (!((CollapseableThemedField) getField(i)).isCollapsed()) numExpandedFields++;
		for (int i = 0; i < getFieldCount(); i++) {
			CollapseableThemedField collapseableThemedField = (CollapseableThemedField) getField(i);
			if (!multipleFieldsOpenSimultaneously && collapseableThemedField != field) collapseableThemedField.collapse();
			if (collapseableThemedField == field) {
				//Check for illegal collapses
				if (collapseableThemedField.isCollapsed() && !isCollapseable() && numExpandedFields == 0) collapseableThemedField.expand();
			}	
		}
		invalidate();
	}

	protected void sublayout(int width, int height) {
		//If USE_ALL_HEIGHT is enabled, height allocated to content field is height - sum(titlebars)
		//Otherwise, height allocated to the content field is the maximum of the required content heights
		int titleFieldSum = 0;
		int contentHeight = 0;
		for (int i = 0; i < getFieldCount(); i++) {
			CollapseableThemedField f = (CollapseableThemedField) getField(i);
			titleFieldSum += f.titleField.getPreferredHeight();
			contentHeight = Math.max(f.contentManager.getPreferredHeight(), contentHeight);
		}
		if ((getStyle() & USE_ALL_HEIGHT) == USE_ALL_HEIGHT) contentHeight = height - titleFieldSum;
		int offset = 0;
		for (int i = 0; i < getFieldCount(); i++) {
			CollapseableThemedField f = (CollapseableThemedField) getField(i);
			int fieldHeight = (f.isCollapsed() ? f.getPreferredHeight() : f.titleField.getPreferredHeight() + contentHeight);
			layoutChild(f, width,  fieldHeight);
			setPositionChild(f, 0, offset);
			offset += fieldHeight;
		}
		setExtent(width, offset);
	}

	/**
	 * Sets the collapseability of the accordion manager.  If it is collapseable, all fields in the accordion
	 * can be collapsed simultaneously. 
	 * @param colleapseable
	 */
	public void setCollapseable(boolean collapseable) {
		this.collapseable = collapseable;
	}

	/**
	 * @return The collapseability of the accordion manager
	 */
	public boolean isCollapseable() {
		return collapseable;
	}
	
}
